'use strict';

const Controller = require('egg').Controller;

class BookController extends Controller {
//增加图书
  async create(){
    const { ctx, service } = this
    const payload = ctx.request.body || {}
    const res=await service.book.create(payload)

    ctx.body = {
      code:200,
      data:res
    }
  }
//图书展示
  async show(){
    const {ctx,service}=this;
    //const payload=ctx.query;//获取路由参数payload
    const res=await service.book.show();
    ctx.body={
      code:200,
      data:res
    }
  }
//书名查找
  async findname() {
    const {ctx,service}=this;
    const _id=ctx.query;//获取路由参数
    console.log(_id);
    const res=await service.book.findname(_id);
    if(res&&res!=""){
      ctx.body={
        code:200,
        data:res
      }
    }else{
      ctx.body='没有'
    }
  }
//兴趣查找
  async findintrest() {
    const {ctx,service}=this;
    const kind=ctx.query;//获取路由参数
    console.log(kind);
    const res=await service.book.findintrest(kind);
    if(res&&res!=""){
      ctx.body=res;
    }else{
      ctx.body='没有'
    }
  }
  // 删除书
  async delete(){
    const{ctx,service}=this;
    const _id=ctx.query;
    const res =await service.book.delete(_id);
    ctx.body={
      msg:'删除成功',
      code:200,
      data:res
    }
  }
  async update(){
    const {ctx,service}=this;
    const _id=ctx.query  ;
    const payload=ctx.request.body ||{};
    const res=await service.book.update(_id,payload);
    ctx.body={
      code:200,
      msg:"修改完成",
      data:res
    }

  }
  //借书
  async borrow(){
    const {ctx,service}=this;
    const _id=ctx.query;
    const res =await service.book.borrow(_id);
    ctx.body={
      code:200,
      data:res
    }
  }
  async return(){
    const {ctx,service}=this;
    const bookname=ctx.query;
    const res =await service.book.return(bookname);
    ctx.body={
      code:200,
      data:res
    }
  }
}

module.exports = BookController;
